import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// eslint-disable-next-line no-unused-vars
let defaultCity = '上海'
try {
  if (localStorage.city) {
    defaultCity = localStorage.city
  }
} catch (e) {}

// 创建公用数据
export default new Vuex.Store({
  state: {
    city: localStorage.city || '北京'
  },
  // 注册aciotns监听
  actions: {
    changeCity (ctx, city) {
      ctx.commit('changeCity', city) // 提交修改和参数
    }
  },
  // 执行方法
  mutations: {
    changeCity (state, city) {
      state.city = city
      // 本地存储
      try {
        if (city) {
          localStorage.city = city
        }
      } catch (e) {}
    }
  }
})
